package java8stream;

import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by ZhuQianli on 2018/1/17.
 */
public class GropingByPartitioningBy {
    public static void main(String[] args) {

        Map<Boolean, List<Person>> collect = Stream.generate(new PersonSupplier()).limit(10).collect(Collectors.partitioningBy(person -> person.getAge() > 5));
        System.out.println(JSON.toJSONString(collect, true));

        Random random = new Random();
        Map<Integer, List<Person>> name = Stream.generate(() -> new Person("name", random.nextInt(4))).limit(10).collect(Collectors.groupingBy(Person::getAge));
        System.out.println(JSON.toJSONString(name));



    }

    static class PersonSupplier implements Supplier<Person> {

        Integer i = 1;

        @Override
        public Person get() {
            return new Person("name" + ++i, i);
        }
    }

    static class Person {
        String name;
        Integer age;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public Person setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public Person setAge(Integer age) {
            this.age = age;
            return this;
        }
    }

}
